package com.gym.myshop.web.controller;

import com.gym.myshop.common.Const;
import com.gym.myshop.entity.Cart;
import com.gym.myshop.entity.User;
import com.gym.myshop.services.CartServices;
import com.gym.myshop.services.impl.CartServicesImpl;
import com.gym.myshop.util.StrUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author ming
 * 2021/11/6
 * description:购物车控制
 */
@WebServlet("/cart")
public class CartController extends BaseServlet {

    private CartServices cs = new CartServicesImpl();

    /**
     *@author:ming
     *@Date: 2021/11/6
     *@description:加入购物车，登录后才能加
     */
    public String createCart(HttpServletRequest request, HttpServletResponse response) {
        //加购物车时验证用户是否登录
        User user = (User) request.getSession().getAttribute(Const.UserConst.IS_LOGIN);
        if (user == null) {
            //用户没有登录，重定向跳转到登录页面
            return Const.REDIRECT + "login.jsp";
        }
        // 已登录的话通过pid商品的唯一主键接收前端提供的数据
        Integer pid = StrUtils.toInt(request.getParameter("pid"));
        //访问业务开始处理购物车的新增
        boolean ans = cs.createCart(pid, user.getUid());
        String msg = "添加失败，人品~~";
        if (ans) {
            msg = "添加成功，马上下单！";
        }
        request.setAttribute("msg", msg);
        return Const.FORWARD + "cartSuccess.jsp";
    }


    /**
     *@author:ming
     *@Date: 2021/11/6
     *@description:查看购物车数据
     */
    public String getCart(HttpServletRequest request, HttpServletResponse response) {
        //用户没有登录，重定向跳转到登录页面
        User user = (User) request.getSession().getAttribute(Const.UserConst.IS_LOGIN);
        if (user == null) {
            //用户没有登录
            return Const.REDIRECT + "login.jsp";
        }
        // 访问业务层 获取购物车数据
        List<Cart> cl = cs.getCarList(user.getUid());
        request.setAttribute("carts", cl);
        return Const.FORWARD + "cart.jsp";
    }

    /**
     *@author:ming
     *@Date: 2021/11/6
     *@description:删除购物车商品数据
     */
    public String delCart(HttpServletRequest request, HttpServletResponse response) {
        try {
            //用户没有登录，重定向跳转到登录页面
            User user = (User) request.getSession().getAttribute(Const.UserConst.IS_LOGIN);
            if (user == null) {
                return Const.REDIRECT + "login.jsp";
            }
            String p = request.getParameter("pid");
            Integer pid = Integer.valueOf(p);
            //调用业务层，删除
            boolean ans = cs.delCart(user.getUid(), pid);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return Const.REDIRECT + "cart?method=getCart";
    }

    /**
     *@author:ming
     *@Date: 2021/11/6
     *@description:修改商品数量
     * 前端方法是一个ajax,并且不要求返回值,直接设置方法为void
     */
    public void updateCart(HttpServletRequest request, HttpServletResponse response) {
        //处理验证用户是否登录
        User user = (User) request.getSession().getAttribute(Const.UserConst.IS_LOGIN);
        Integer pid = StrUtils.toInt(request.getParameter("pid"));
        Integer num = StrUtils.toInt(request.getParameter("num"));
        //访问业务层操作修改
        cs.updateCart(user.getUid(), pid, num);
    }

}
